﻿using JewererBasic.Classes;
using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;

namespace JewererBasic.BO
{
    public class ToolBO
    {
        public static bool Add(TOOL tool)
        {
            MSQDBEntities db = new MSQDBEntities();
            var query = from d in db.TOOLs
                        where d.Id.Equals(tool.Id)
                        select d;
            if (query.Count() > 0)
                return false;
            db.TOOLs.Add(tool);
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                // validation error
                return false;
            }
            return true;
        }

        public static List<TOOL> Search(string id)
        {
            MSQDBEntities db = new MSQDBEntities();
            var query = (from d in db.TOOLs
                         where d.Id.Contains(id)
                         select d).ToList();
            return query;
        }

        public static List<TOOL> Moulds()
        {
            MSQDBEntities db = new MSQDBEntities();
            return db.TOOLs.ToList();
        }

        public static bool Import(TOOL_IMPORT toolImport, List<TOOL_IMPORT_DETAIL> details)
        {
            using (MSQDBEntities db = new MSQDBEntities())
            {
                // use transaction to prevent error on data.
                using (var scope = new TransactionScope())
                {
                    try
                    {
                        // add TOOL_Import
                        toolImport.Creator = CurrentUser.UserID;
                        toolImport.DayMake = DateTime.Now.Date;
                        db.TOOL_IMPORT.Add(toolImport);
                        db.SaveChanges();

                        // add Detail Stone Import
                        for (int i = 0; i < details.Count; i++)
                        {
                            details[i].IdImport = toolImport.Id;
                            db.TOOL_IMPORT_DETAIL.Add(details[i]);
                        }
                        db.SaveChanges();

                        // add weight and numb to stone
                        for (int i = 0; i < details.Count; i++)
                        {
                            var query = db.TOOLs.Find(details[i].IdTool);
                            if (query == null)
                                return false;
                            query.Quantity += details[i].Quantity;
                        }
                        db.SaveChanges();
                        scope.Complete();
                    }
                    catch (Exception e)
                    {
                        return false;
                    }
                }
                return true;
            }
        }

        public static bool Export(TOOL_EXPORT se, List<TOOL_EXPORT_DETAIL> details)
        {
            using (MSQDBEntities db = new MSQDBEntities())
            {
                using (var scope = new TransactionScope())
                {
                    try
                    {
                        // create stone exportstaff
                        se.DayMake = DateTime.Now.Date;
                        se.Creator = CurrentUser.UserID;
                        db.TOOL_EXPORT.Add(se);
                        db.SaveChanges();
                        for (int i = 0; i < details.Count; i++)
                        {
                            details[i].IdExport = se.Id;
                            db.TOOL_EXPORT_DETAIL.Add(details[i]);
                        }
                        db.SaveChanges();

                        // subtractStone
                        for (int i = 0; i < details.Count; i++)
                        {
                            var query = db.TOOLs.Find(details[i].IdTool);
                            if (query != null)
                            {
                                query.Quantity -= details[i].Quantity;
                            }
                        }
                        db.SaveChanges();
                        scope.Complete();
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }
                }
                return true;
            }
        }

        public static List<HistoryTool> History(string idTool, DateTime? dateFrom, DateTime? dateTo, bool isImport, bool isExport)
        {
            List<HistoryTool> result = new List<HistoryTool>();
            MSQDBEntities db = new MSQDBEntities();

            // get import history
            if (isImport == true)
            {
                var queryImport = from d in db.TOOL_IMPORT
                                  select d;

                if (dateFrom.HasValue)
                {
                    queryImport = queryImport.Where(d => d.DayMake >= dateFrom.Value);
                }
                if (dateTo.HasValue)
                {
                    queryImport = queryImport.Where(d => d.DayMake <= dateTo.Value);
                }

                var imports = queryImport.ToList();
                if (imports.Count > 0)
                {
                    foreach (var item in imports)
                    {
                        foreach (var tool in item.TOOL_IMPORT_DETAIL)
                        {
                            if (String.IsNullOrEmpty(idTool) == true || tool.IdTool.Equals(idTool))
                            {
                                result.Add(new HistoryTool(item, tool));
                            }
                        }
                    }
                }
            }

            // get import history
            if (isExport == true)
            {
                var queryImport = from d in db.TOOL_EXPORT
                                  select d;

                if (dateFrom.HasValue)
                {
                    queryImport = queryImport.Where(d => d.DayMake >= dateFrom.Value);
                }
                if (dateTo.HasValue)
                {
                    queryImport = queryImport.Where(d => d.DayMake <= dateTo.Value);
                }

                var imports = queryImport.ToList();
                if (imports.Count > 0)
                {
                    foreach (var item in imports)
                    {
                        foreach (var tool in item.TOOL_EXPORT_DETAIL)
                        {
                            if (String.IsNullOrEmpty(idTool) == true || tool.IdTool.Equals(idTool))
                            {
                                result.Add(new HistoryTool(item, tool));
                            }
                        }
                    }
                }
            }

            
            return result;
        }
    }
}